package com.example.administrator.gilde_demo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView lv = (GridView) findViewById(R.id.lv);

        String[] list = new String[]{
                "http://img01.store.sogou.com/app/a/10010016/25b5bd5fbf58fdb8ca3471ac0ff4ca143",
                //下面是一个gif图片,Glide接受Gif或者图片作为load()参数
                "http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif",
                "http://img02.store.sogou.com/app/a/10010016/370fbccef1effa77a15562fcfe4a76cd",
                "http://img01.store.sogou.com/app/a/10010016/4740370fe89029f734f8e9b82ee7d9b8",
                "http://img03.store.sogou.com/app/a/10010016/efff56af09a1361f73737675b3fee539",
                "http://img01.store.sogou.com/app/a/10010016/0ef17d07e7b6d0b96a6ef8b6dc23b59f",
                "http://img03.store.sogou.com/app/a/10010016/24240ea71ea2896940bc09d7cb497e9d",
                "http://img02.store.sogou.com/app/a/10010016/412bba13240c085ce15cbb232e036bd7",
                "http://img03.sogoucdn.com/app/a/100520093/ea54b1c5225b5b8f-1f7d693ce5c84217-65bcb52a88b749bfa5bd038796a7f699.jpg",
                "http://img02.store.sogou.com/app/a/10010016/d611da5c4d925c615f14a88e9fe0120c",
                "http://www.qq1234.org/uploads/allimg/121119/1_121119144404_1.jpg",
                "http://img04.store.sogou.com/app/a/10010016/e4e5e60310c4e94c84411a0da907fc6f",
                "http://img04.store.sogou.com/app/a/10010016/072cb8c9a057106fab80b812f42a9a53"
        };

        lv.setAdapter(new Mybs(MainActivity.this, list));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });

    }

    class Mybs extends BaseAdapter {

        Context context;
        String[] list;
        private ViewHolder vh;

        public Mybs(Context context, String[] list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.length;
        }

        @Override
        public Object getItem(int position) {
            return list[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.item, null);
                vh = new ViewHolder();
                vh.im = (ImageView) convertView.findViewById(R.id.im);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }

            //*****
            Glide
                    .with(context)//上下文
                    .load(list[position])//图片url
                    .placeholder(R.mipmap.ic_launcher)//加载前图片
                    .error(R.drawable.imim)//加载错误图片
                    //.dontAnimate()//不显示淡入淡出效果
                    //.override(100,200)//如果图片不会自动适配到ImageView,调用override(horizontalSize,verticalSize).这将在图片显示到ImageView之前重新改变图片大小。
                    //.centerCrop()//填满控件,但是图片显示可能不完全
                    //.fitCenter()//图片完全显示,但是控件会有黑边
                    //.asGif()//额外的防区强制Glide变成一个Gif ???
                    .crossFade(2000)//淡入淡出效果和持续时间(可以没有参数)
                    //.asBitmap()//Gif 转为 Bitmap ???
                    //.skipMemoryCache(true)//Glide跳过内存缓存,存入磁盘
                    //.diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存<最好和禁用内存一起使用>
                    //.priority(Priority.HIGH)//加载优先级
                    //.thumbnail(0.1f)//简单缩略图
                    .into(vh.im);//将效果装载到图片控件

            /**
             * 对于 .diskCacheStrategy() 方法来说不同的枚举参数的意义：

             DiskCacheStrategy.NONE 什么都不缓存，就像刚讨论的那样
             DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像。在我们上面的例子中，将会只有一个 1000x1000 像素的图片
             DiskCacheStrategy.RESULT 仅仅缓存最终的图像，即，降低分辨率后的（或者是转换后的）
             DiskCacheStrategy.ALL 缓存所有版本的图像（默认行为）
             */

            /**
             * 优先级枚举参数
             *  Priority.LOW
             Priority.NORMAL<正常>
             Priority.HIGH
             Priority.IMMEDIATE<立刻>
             */


            return convertView;
        }

        class ViewHolder {
            ImageView im;
        }
    }
}
